package org.swim.core.manager;

import java.util.List;

import javax.ejb.EJBException;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.swim.remote.entities.ICollaboration;
import org.swim.remote.entities.IUser;
import org.swim.remote.manager.AbilityManagerRemote;
import org.swim.remote.manager.CollaborationManagerRemote;
import org.swim.remote.manager.FriendshipManagerRemote;
import org.swim.remote.manager.UserManagerRemote;
import org.swim.utils.SwimLog;

public class Main {

	static {
		System.setProperty("java.security.auth.login.config", "jaas.config");
	}

	/**
	 * @param args
	 */
	@SuppressWarnings("unused")
	public static void main(String[] args) {

		Context ic, ic2, ic3, ic4;

		System.getProperties().put("java.naming.factory.initial",
				"org.jnp.interfaces.NamingContextFactory");
		System.getProperties().put("java.naming.provider.url",
				"jnp://localhost");

		try {

			ic = new InitialContext();
			ic2 = new InitialContext();
			ic3 = new InitialContext();
			ic4 = new InitialContext();
			UserManagerRemote uml = (UserManagerRemote) ic
					.lookup("swim/UserManager/remote");
			AbilityManagerRemote aml = (AbilityManagerRemote) ic2
					.lookup("swim/AbilityManager/remote");
			
			FriendshipManagerRemote fml = (FriendshipManagerRemote) ic3
					.lookup("swim/FriendshipManager/remote");
			
			CollaborationManagerRemote cml = (CollaborationManagerRemote) ic4
					.lookup("swim/CollaborationManager/remote");
			
			//SwimEntityManager<IUser> suml = (SwimEntityManager<IUser>) ic
				//	.lookup("swim/UserManager/remote");

			// />>>>>>>>>>>>LAVORARE DA QUI<<<<<<<<<<<<<<<<<<<<<<<<//
			/*List<IUser> users = uml.getAll();
			for (IUser iUser : users) {
				System.out.println(iUser.getName() + " - feedback avarage: "
						+ iUser.getFeedbackAvgVal());
						}*/
					
			
			JSONObject o = new JSONObject();
			JSONObject obj = new JSONObject();
			JSONObject obj22 = new JSONObject();
			JSONArray jArr = new JSONArray();
			
			
			/*
			 * USER
			 * SIGNIN - SIGNUN - SIGNOUT
			 */
//			o.put(IUser.Attribute.NAME, "Chiara");
//			o.put(IUser.Attribute.SURNAME, "Dell'Orto");
			o.put(IUser.Attribute.EMAIL, "chiara@aaa.a");
//			o.put(IUser.Attribute.EMAIL, "marcofuma89@gmail.com");
//			o.put(IUser.Attribute.EMAIL, "federico.monterisi@gmail.com");

//			o.accumulate("senderEmail", "cicci@aaaa.a");
			
//			o.put(IUser.Attribute.STATUS, Status.REGISTERED.name());
//			SwimLog.getSwimLog().debug(UserManager.class,o.toString(4));
//			obj.put(IUser.Attribute.EMAIL, "chiara@aaaaa.a");
			o.put( IUser.Attribute.PASSWORD, "a");
//			o.put( IUser.Attribute.PASSWORD, "olimpia");
//			obj.accumulate(IUser.Attribute.EMAIL.toString(), "fede.aa@a.a");
//			obj.accumulate(IUser.Attribute.PASSWORD.toString(), "aaa");
			
			///LOGIN//
			String userToken = uml.signIn(o);
			o.clear();
			o.accumulate("token", userToken);
			
			obj.accumulate("token", userToken);
//			obj.accumulate("id", 1);
//			obj.accumulate("status", INotification.NotificationStatus.READ);
			
//			fml.setStatusFriendshipNotification(obj);
//			
//			List<INotification> notif = uml.getLastNotification(o);
//			for (INotification iNotification : notif) {
//				SwimLog.getSwimLog().debug(Main.class,"Notif: " + iNotification.getNotificationType() + " - Date: " + iNotification.getTimestamp().toString());
//			}
			
			
			///COLLABORATION///
//			obj.accumulate("title", "Senza Amicizia");
//			obj.accumulate("receiverId", 9);
//			obj.accumulate("senderVisibility", Visibility.VISIBLE);
//			o.accumulate("collaboration", obj);
//			obj22.accumulate("message", "prova messaggio");
//			o.accumulate("collaborationId", 6);
//			o.accumulate("message", obj22);
			
//			cml.postMessage(o);
			
//			cml.createCollaboration(o);
			
//			o.accumulate("collaborationId", 4);
//			o.accumulate("status",Status.COMPLETED);
			
			//cml.setStatusCollaboration(o);
//			SwimLog.getSwimLog().debug(Main.class,o.toString(4));
			
//			obj.accumulate("token", userToken);
//			obj22.accumulate("collaborationId", 6);
//			obj22.accumulate("comment", "valutazione valida");
//			obj22.accumulate("valuation", 5);
			
//			obj.accumulate("feedbackId", 1);
			
//			cml.deleteFeedback(obj);
//			
			List<ICollaboration> message = cml.getMyCollaborations(o);
			
			for (ICollaboration iMessage : message) 
				{
					SwimLog.getSwimLog().debug(Main.class,"CollId: " + iMessage.getId() + " - Message num: " + iMessage.getMessagesList().size() );
				}
			
//			obj.accumulate("friendshipId", 3);
//			obj.accumulate("status", IFriendship.Status.CONFIRMED);
			
//			fml.requestFriendship(obj);
//			fml.setStatusFriendship(obj);
//			jArr.add("FEDERICO");
//			jArr.add("EHI FEDE");
//			obj.accumulate("abilities", jArr);
//			obj.accumulate("user", o);
//			obj.accumulate("query", "FEDERICO");
			
//			List<IUser> users = uml.findFriends(obj);
//			
//			SwimLog.getSwimLog().debug(Main.class,obj.toString(4));
//			
//			uml.updateUser(obj);
			
//			obj.getString("CASA");
//			uml.signIn(obj);
//			String user2 = uml.signIn(obj);
//			o.put("token", user);
//			String user3 = uml.signIn(obj);
//			SwimLog.getSwimLog().debug(UserManager.class,user);
//			SwimLog.getSwimLog().debug(UserManager.class,user2);
//			uml.signOut(o);
			
			
			/*
			 * ABILITY
			 * FINDABILITIES -- ADDABILITY
			 */
//			obj.accumulate("token", userToken);
//			obj22.accumulate(IAbility.Attribute.NAME.toString(), "EHI FEDE");
//			obj.accumulate("search-query", "EHI FEDE");
			
//			aml.addNewAbility(obj);
//			SwimLog.getSwimLog().debug(UserManager.class,"fff");
//			uml.addAbility(obj);
//			
//			o.accumulate("search-query", "f");
//			List<IAbility> abilities =  aml.findAbilities(obj);
			
//			SwimLog.getSwimLog().debug(UserManager.class, JSONArray.fromObject(abilities).toString());
//			SwimLog.getSwimLog().printList(AbilityManager.class, abilities);
			
//			for (IAbility iAbility : abilities) 
//			{
//				SwimLog.getSwimLog().debug(Main.class,"TUTTE: " + iAbility.getName());
//			}
//			
//			IUser user = uml.getTokenOwner(userToken);
//			SwimLog.getSwimLog().debug(Main.class,"UTENTE: " + user.getFullName());
//			for (IAbility ability : user.getAbilitiesSet()) 
//			{
//				SwimLog.getSwimLog().debug(Main.class,"UTENTE: " + ability.getName());
//			}
			
//			uml.removeAccount(o);
			
//			List<IUser> users = uml.findUsers(obj22);
//			
//			SwimLog.getSwimLog().debug(Main.class,"NUM UTENTI TORVATI: " + users.size());
//			
//			for (IUser iUser : users) 
//			{
//				SwimLog.getSwimLog().debug(Main.class,"UTENTE TORVATO: " + iUser.getFullName());
//			}

				// />>>>>>>>>>>>LAVORARE FINO A QUI<<<<<<<<<<<<<<<<<<<<<<<<//
			

		
		/*} catch (UserException e) {
			//e.printStackTrace();
			SwimLog.getSwimLog().debug(UserManager.class,"ERRORE UTENTE:  " + e.getMessage());
		*/
		}catch (NamingException e) {
			e.printStackTrace();
			//SwimLog.getSwimLog().debug(UserManager.class,"ERRORE NOME");
			
		} catch (EJBException e) {
			e.printStackTrace();
			//System.out.println("DOPPIO INSERIMENTO");
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
